<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
    <style>
        .layui-form-select dl{
            max-height: 130px;
        }
    </style>
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">审批</a>
            <a>
                <cite>审批</cite></a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-d layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form livingList">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">月份</label>
                                <div class="layui-input-block">
                                    <input type="text" autocomplete="off" placeholder="请选择月份" class="layui-input"
                                           id='month'>
                                </div>
                            </div>
<!--                            <div class="layui-inline">-->
<!--                                <label class="layui-form-label">搜索</label>-->
<!--                                <div class="layui-input-block">-->
<!--                                    <input type="search" autocomplete="off" placeholder="输入项目名称" class="layui-input"-->
<!--                                           id='searchInput'>-->
<!--                                </div>-->
<!--                            </div>-->
                            <div class="layui-inline">
                                <button class="layui-btn search" type="button" id='search'>
                                    <i class="iconfont layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type='text/html' id='toolbar'>
    <button class="layui-btn launch" type="button" auth='approvalLaunch'>发起审核</button>
</script>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['form', 'layer', "table", "laydate"], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
        laydate.render({
            elem: '#month'
            , type: 'month'
            , value: year + '-' + month
            , max: 0
        });
        // 异步数据加载
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '项目列表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/approval/approval/processLists', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            method: 'POST',
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            where: {
                month: year + '-' + month,
                search: "",
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter'],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    {field: '', title: '编号', type: 'numbers', width: '80'},
                    {field: 'name', title: '项目名称', width: "240",},
                    {field: 'startTime', title: '起始时间', width: "100",templet:function(d){
                        return d.startTime.substr(0,10);
                        }},
                    {field: 'endTime', title: '起始时间', width: "100",templet:function(d){
                            return d.endTime.substr(0,10);
                        }},
                    {
                        field: 'processList', title: '账目提交情况', width: "360", templet: function (d) {
                            var html = ''
                            for (var i = 0; i < d.processList.length; i++) {
                                if (d.processList[i].roleId == getParam('roleId') && d.processList[i].status == 0){
                                    processStatus = 0;
                                }
                                if (d.processList[i].status == 0) {
                                    html += `<span style='color:red'>${d.processList[i].roleName}</span>&nbsp;&nbsp;`
                                } else {
                                    html += `<span style='color:darkcyan'>${d.processList[i].roleName}</span>&nbsp;&nbsp;`
                                }
                            }
                            return html;
                        }
                    },
                    {
                        field: 'status', title: '状态', width: "120", templet: function (d) {
                            switch (d.status) {
                                case 0:
                                    return "账目收集";
                                case 1:
                                    return "待审核";
                                case 2:
                                    return "审核中";
                                case 3:
                                    return "通过";
                            }
                        }
                    },
                    {
                        field: '', title: '操作', width: "120", templet: function (d) {
                            if(d.status == 2){
                                html = `<button class='approval layui-btn' data-id=${d.projectId} auth='approval' lay-event='approval'>账目审核</button>`
                                return html;
                            }else{
                                return `<button class='approval layui-btn' data-id=${d.projectId} auth='approval' lay-event='approval'>账目明细</button>`;
                            }
                        }
                    }
                ]
            ],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            done: function () {
                permission();
            }
        });
        table.on('tool(test)', function (data) {
            if (data.event == 'approval') {
                window.location.href = 'approvalData.html?approvalId=' + data.data.id+'&projectId='+data.data.projectId;
            }
        })

        //搜索
        $('body').on('click', '#search', function () {
            var search = $(this).parent().prev().find('input').val();
            table.reload('lists', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    month: $('#month').val(),
                    token: getToken(),
                    search: search
                }
            })
        })

        $('body').on('click', 'button.launch', function () {
            let option = `<option value='0'>全部项目</option>`;
            $.ajax({
                url: url + '/project/project/selectLists',
                type: "POST",
                async: false,
                data: {
                    isAll: 0,
                    token: getToken()
                },
                success: function (r) {
                    if (r.code == 100000) {
                        for (var i = 0; i < r.data.length; i++) {
                            option += `<option value=${r.data[i].id}>${r.data[i].name}</option>`
                        }
                    } else {
                        tips('项目下拉列表', r);
                    }
                }
            });
            layer.open({
                type: 1,
                title: "审核",
                area: ['40%', '60%'],
                content: `<form class='layui-form layerOpen' lay-filter="roleForm">
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>起始时间</label>
                                <div class='layui-input-block' >
                                    <input type="text" autocomplete="off"  class="layui-input" id='startTime' >
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>截止时间</label>
                                <div class='layui-input-block' >
                                    <input type="text" autocomplete="off"  class="layui-input" id='endTime'>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>项目</label>
                                <div class='layui-input-block' >
                                    <select name="projectId" id="projectId" lay-search>${option}</select>
                                </div>
                            </div>
                        </div>
                    </form>`,
                btn: ['确认', '取消'],
                skin: 'my-skin',
                success:function(){
                    laydate.render({
                        elem: '#startTime'
                        , type: 'date'
                        , max: 0
                    });
                    laydate.render({
                        elem: '#endTime'
                        , type: 'date'
                        , max: 0
                    });
                    form.render();
                },
                yes: function (index, layero) {
                    $.ajax({
                        url: url + "/approval/approval/initiateApplication",
                        type: "POST",
                        async: false,
                        data: {
                            startTime: $('#startTime').val(),
                            endTime: $('#endTime').val(),
                            projectId:$('#projectId').val(),
                            token: getToken()
                        },
                        success: function (r) {
                            if (r.code == 100000) {
                                tips('审核', r);
                                setTimeout(function () {
                                    layer.closeAll();
                                    table.reload('lists');
                                }, 2000)
                            } else {
                                tips('审核', r);
                            }
                        }
                    })
                }
            })
        })
    });

</script>
</html>